package com.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * 时间日期工具
 */
public class DateTimeUtil {
	private static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
	private static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
	
	private DateTimeUtil() {}

	/**
	 * 将LocalDateTime转换为指定的时间字符串
	 */
	public static String formatLocalDateTime(LocalDateTime localDateTime, String pattern) {
		if (Objects.isNull(localDateTime)) {
			return "";
		}
		if (Objects.isNull(pattern)) {
			pattern = DEFAULT_DATE_TIME_FORMAT;
		}
		return DateTimeFormatter.ofPattern(pattern).format(localDateTime);
	}

	/**
	 * 将LocalDate转换为指定的时间字符串
	 */
	public static String formatLocalDate(LocalDate localDate, String pattern) {
		if (Objects.isNull(localDate)) {
			return "";
		}
		if (Objects.isNull(pattern)) {
			pattern = DEFAULT_DATE_FORMAT;
		}
		return DateTimeFormatter.ofPattern(pattern).format(localDate);
	}

	/**
	 * 将LocalTime转换为指定的时间字符串
	 */
	public static String formatLocalTime(LocalTime localTime, String pattern) {
		if (Objects.isNull(localTime)) {
			return "";
		}
		if (Objects.isNull(pattern)) {
			pattern = DEFAULT_TIME_FORMAT;
		}
		return DateTimeFormatter.ofPattern(pattern).format(localTime);
	}
}